package com.taxigang.client;

import com.google.code.gwt.geolocation.client.Geolocation;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.Geolocator;
import com.taxigang.client.ui.map.JoinView;
import com.taxigang.client.ui.map.MapView;
import com.taxigang.client.ui.map.ProcessingDialog;
import com.taxigang.client.uibinder.TaxiGangHome;
import com.taxigang.client.uibinder.TaxiGangPresenter;
import com.taxigang.client.uibinder.TaxiGangRoute;

public class TaxiGangRouteLoader implements Command{

	private TaxiGangHome taxiGangHome;

	public void setTaxiGangHome(TaxiGangHome taxiGangHome){
		this.taxiGangHome = taxiGangHome;
	}
	@Override
	public void execute() {
		GWT.runAsync(new RunAsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert("Code download failed");
			}

			public void onSuccess() {
				TaxiGangRoute taxiGangRoute = new TaxiGangRoute();
				if (taxiGangHome != null){
					RootPanel.get().remove(taxiGangHome);
				}

				RootPanel.get().add(taxiGangRoute);
				TaxiGangProcessor.authenticate();

				taxiGangRoute.showMenu();
				initPresenter(taxiGangRoute, taxiGangRoute);
				TaxiGangMain.hideLoading();
				
				//for geolocation
				ProcessingDialog.show(TaxiGangCreator.taxiGangConstants.processing());

			}
		});

	}


	public void cancelWishRoute(){
		Command cmd = new Command() {

			@Override
			public void execute() {
				TaxiGangRoute taxiGangRoute = new TaxiGangRoute();
				ProcessingDialog.show(TaxiGangCreator.taxiGangConstants.loading());

				taxiGangRoute.setPanelToShowOnLogin(null);
				RootPanel.get().add(taxiGangRoute);
				TaxiGangHandler.getInstance().showWishRouteCancel();
				TaxiGangProcessor.authenticate();
				taxiGangRoute.showMenu();

				TaxiGangMain.hideLoading();
				initPresenter(taxiGangRoute, taxiGangRoute);

			}
		};
		CodeLoader.loadAndExecute(cmd);
	}


	public void showInviation(){
		Command cmd = new Command(){

			@Override
			public void execute() {
				TaxiGangRoute taxiGangRoute = new TaxiGangRoute();
				ProcessingDialog.show(TaxiGangCreator.taxiGangConstants.loading());

				taxiGangRoute.setPanelToShowOnLogin(null);
				RootPanel.get().add(taxiGangRoute);
				TaxiGangHandler.getInstance().showInvitation();
				TaxiGangProcessor.authenticate();
				taxiGangRoute.showMenu();

				TaxiGangMain.hideLoading();

				initPresenter(taxiGangRoute, taxiGangRoute);

			}

		};
		CodeLoader.loadAndExecute(cmd);
	}

	private void initPresenter(MapView mapView, JoinView joinView){
		TaxiGangPresenter taxiGangPresenter = TaxiGangPresenter.getInstance();
		taxiGangPresenter.setMapView(mapView,joinView);

	}
}
